<template>
  <el-container class="home-container" v-show="!loading" v-loading="loading">
    <el-header>
      <h3>电商后台管理系统</h3>
      <el-button type="info" @click="logout">退出登录</el-button>
    </el-header>
    <el-container>
      <aside-menu :asideListData="asideListData"></aside-menu>
      <el-main>
        <!-- 显示子路由 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
// @ is an alias to /src
import AsideMenu from 'components/AsideMenu'
export default {
  name: 'Home',
  components: {
    AsideMenu
  },
  data() {
    return {
      asideListData: {
        menuList: []
      },
      loading: true
    }
  },
  created() {
    this.getMenuList()
  },
  methods: {
    logout() {
      // 清空sessionStorage，再跳转首页
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    getMenuList() {
      this.axios
        .get('menus')
        .then(res => {
          const data = res.data
          if (data.meta.status !== 200) {
            this.$message.error(data.meta.msg)
            this.$router.push('/login')
          } else {
            this.asideListData.menuList = data.data
            this.loading = false
          }
        })
        .catch(err => {
          console.error(err)
        })
    }
  }
}
</script>
<style lang="scss" scoped>
.home-container {
  height: 100%;
  .el-header {
    background-color: #373d41;
    display: flex;
    align-items: center;
    justify-content: space-between;
    h3 {
      font-size: 20px;
      font-weight: 500;
      color: #fff;
    }
  }

  .el-main {
    background-color: #eaedf1;
  }
}
</style>
